<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/12/10
 * Time: 15:43
 */

namespace app\home\controller;


use app\home\logic\CartLogic;

class Cart extends Base
{
    public $cartLogic; // 购物车逻辑操作类
    private $user = false;

    public function _initialize(){
        parent::_initialize();
        $this->cartLogic = new CartLogic();
        $this->user = $this->user();
    }

    //ajax添加购物车
    public function ajaxAddCart(){
        if (!$this->user) ajaxReturn(array('status' => 401,'msg' => '请先登录后再尝试操作','url' => U('User/login',array('type' => 2))));
        $goods_id = I("goods_id/d",0); // 商品id
        $goods_num = I("goods_num/d",0);// 商品数量
        $goods_spec_key = I("goods_spec"); // 商品规格
        if(empty($goods_id)){
            ajaxReturn(array('status' => 500,'msg' => '商品参数错误，请联系管理人员'));
        }
        if(empty($goods_num)){
            ajaxReturn(array('status' => 500,'msg' => '商品数量不能为0'));
        }
//        if (!$goods_spec_key) {
//            ajaxReturn(array('status' => 500,'msg' => '请选择你想要的规格'));
//        }
        $this->cartLogic->setGoodsModel($goods_id);
        $this->cartLogic->setUserId($this->user['user_id']);
        $result = $this->cartLogic->addGoodsToCart($goods_num, $goods_spec_key, 1); // 将商品加入购物车
        ajaxReturn($result);
    }

    //订餐 ajax添加购物车
    public function ajaxAddCart2(){
        if (!$this->user) ajaxReturn(array('status' => 401,'msg' => '请先登录后再尝试操作','url' => U('User/login',array('type' => 3))));
        $goods_id = I("goods_id/d",0); // 商品id
        $goods_num = I("goods_num/d",0);// 商品数量
        $goods_spec_key = I("goods_spec"); // 商品规格
        if(empty($goods_id)){
            ajaxReturn(array('status' => 500,'msg' => '商品参数错误，请联系管理人员'));
        }
        if(empty($goods_num)){
            ajaxReturn(array('status' => 500,'msg' => '商品数量不能为0'));
        }
        $this->cartLogic->setGoodsModel($goods_id);
        $this->cartLogic->setUserId($this->user['user_id']);
        $result = $this->cartLogic->addGoodsToCart2($goods_num, $goods_spec_key, 1); // 将商品加入购物车
        if ($result['status'] == 200){
            $result['url'] = U('Goods/order_index');
        }
        ajaxReturn($result);
    }


    //购物车首页
    public function index(){
        if (!$this->user) $this->redirect('User/login',array('type' => 2));
        $this->assign('title','购物车');
        return $this->fetch();
    }

    public function ajax_index(){
        if (!$this->user) ajaxReturn(array('status' => 401,'msg' => '请先登录后再尝试操作','url' => U('User/login',array('type' => 2))));
        $post_goods_num = I("num/a");
        $where['user_id'] = $this->user['user_id'];
        $cartList = M('Cart')->where($where)->getField("id,goods_num");
        if (!empty($post_goods_num) && is_array($post_goods_num)){
            // 修改购物车数量
            foreach ($post_goods_num as $key => $val) {
                $data['goods_num'] = $val < 1 ? 1 : $val;
                if ($cartList[$key]['goods_num'] != $data['goods_num']){
                    M('Cart')->where("id", $key)->save($data);
                }
            }
        }
        $this->cartLogic->setUserId($this->user['user_id']);
        $result = $this->cartLogic->getUserCartList(1);
        if(empty($result['total_price'])){
            $result['total_price'] = array( 'total_fee' =>0, 'num' => 0);
        }
        foreach ($result['cartList'] as $k=>$v){
            $result['cartList'][$k]['original_img'] = M('goods')->where(['goods_id' => $result['cartList'][$k]['goods_id']])->getField('original_img');
        }
        $this->assign('cartList', $result['cartList']);
        $this->assign('cartCount', count($result['cartList']));
        $this->assign('total_price', $result['total_price']); // 总计
        return $this->fetch();
    }

    //订餐购物车页面
    public function ajax_meal_index(){
        //if (!$this->user) ajaxReturn(array('status' => 401,'msg' => '请先登录后再尝试操作','url' => U('User/login',array('type' => 3))));
        $post_goods_num = I("num/a");
        $where['user_id'] = $this->user['user_id'];
        $cartList = M('meal_cart')->where($where)->getField("id,goods_num");
        if (!empty($post_goods_num) && is_array($post_goods_num)){
            // 修改购物车数量
            foreach ($post_goods_num as $key => $val) {
                $data['goods_num'] = $val < 1 ? 1 : $val;
                if ($cartList[$key]['goods_num'] != $data['goods_num']){
                    M('meal_cart')->where("id", $key)->save($data);
                }
            }
        }
        $this->cartLogic->setUserId($this->user['user_id']);
        $result = $this->cartLogic->getUserCartList2(1);
        if(empty($result['total_price'])){
            $result['total_price'] = array( 'total_fee' =>0, 'num' => 0);
        }
        foreach ($result['cartList'] as $k=>$v){
            $result['cartList'][$k]['original_img'] = M('goods')->where(['goods_id' => $result['cartList'][$k]['goods_id']])->getField('original_img');
            $result['cartList'][$k]['goods_remark'] = M('goods')->where(['goods_id' => $result['cartList'][$k]['goods_id']])->getField('goods_remark');
        }
        $this->assign('cartList', $result['cartList']);
        $this->assign('cartCount', count($result['cartList']));
        $this->assign('total_price', $result['total_price']); // 总计
        return $this->fetch();
    }

    //切换选中状态
    public function tab_status(){
        if (!$this->user) ajaxReturn(array('status' => 401,'msg' => '请先登录后再尝试操作','url' => U('User/login',array('type' => 2))));
        $item_id = I('param.item_id');
        $selected = M('cart')->where(array('id' => $item_id))->getField('selected');
        if ($selected == 0){
            M('cart')->where(array('id' => $item_id))->save(array('selected' => 1));
        }elseif ($selected == 1){
            M('cart')->where(array('id' => $item_id))->save(array('selected' => 0));
        }
        ajaxReturn(array('status' => 200));
    }

    //切换全选状态
    public function tab_qx(){
        if (!$this->user) ajaxReturn(array('status' => 401,'msg' => '请先登录后再尝试操作','url' => U('User/login',array('type' => 2))));
        $selected = I('param.selected');
        M('cart')->where(array('user_id' => $this->user['user_id']))->save(array('selected' => $selected));
        ajaxReturn(array('status' => 200));
    }

    //删除选中购物车
    public function del_cart(){
        if (!$this->user) ajaxReturn(array('status' => 401,'msg' => '请先登录后再尝试操作','url' => U('User/login',array('type' => 2))));
        $data = trim_array_element(I('param.'));
        $where = array();
        if (!empty($data['cart_id'])){
            $where['id'] = array('in',$data['cart_id']);
        }
        M('cart')->where($where)->delete();
        ajaxReturn(array('status' => 200));
    }


    //订餐购物车清空
    public function empty_cart(){
        if (!$this->user) ajaxReturn(array('status' => 401,'msg' => '请先登录后再尝试操作','url' => U('User/login',array('type' => 3))));
        M('meal_cart')->where(array('user_id' => $this->user['user_id']))->delete();
        ajaxReturn(array('status' => 200,'msg' => '已清空','url' => U('Goods/order_index')));
    }
    
    
    //删除点餐购物车单个
    public function del_meal_goods(){
    	if (!$this->user) ajaxReturn(array('status' => 401,'msg' => '请先登录后再尝试操作','url' => U('User/login',array('type' => 3))));
    	$item_id = I('param.item_id');
    	M('meal_cart')->where(array('id' => $item_id))->delete();
    	ajaxReturn(array('status' => 200,'msg' => '删除成功'));
    }

}